package org.artifact.core.plugin.netty.handler;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

public class BinaryWebSocketFrameHandler extends AbstractHandler<BinaryWebSocketFrame>{
	
	static final Log log = LogFactory.get(BinaryWebSocketFrameHandler.class);
	
	@Override  
    protected void channelRead0(ChannelHandlerContext ctx, BinaryWebSocketFrame msg) throws Exception {  
        log.info("服务器接收到二进制消息.");  
        ByteBuf content = msg.content();  
        content.markReaderIndex();  
        int flag = content.readInt();  
        log.info("标志位:[{}]", flag);  
        content.resetReaderIndex();  
  
        ByteBuf byteBuf = Unpooled.directBuffer(msg.content().capacity());  
        byteBuf.writeBytes(msg.content());  
  
        ctx.writeAndFlush(new BinaryWebSocketFrame(byteBuf));  
    }  

}
